/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bice.backbean;

import com.bice.entity.User;
import com.bice.service.UserService;
import java.util.List;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.springframework.stereotype.Controller;

    
/**
 *
 * @author sirius
 */
@ManagedBean
@ViewScoped
@Controller
public class IndexBean extends BackBean {
 
    @Resource
    private UserService service;
    
    public IndexBean() {
        user = new User();
    }
    //</editor-fold>
    
    private User user;

    public User getUser() {
        return user;
    }
    
    public boolean isLoggedIn() {
        return getSessionBean().isLogged();
    }

    public void login() {
        User newUser = getUserService().getUser(user.getNickname());
        if(newUser == null) {
            newUser = new User();
            newUser.setNickname(user.getNickname());
            getUserService().save(newUser);
        }
        getSessionBean().setUser(newUser);
        user = new User();
    }
    
    public void disconnect() {
        getSessionBean().disconnect();
    }
    
    public UserService getUserService() {
        return service;
    }
    
}